<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* ol-mapbox-style (https://github.com/boundlessgeo/ol-mapbox-style)
*********************************************************************-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_mvtVectorLayer_mapboxStyle"></title>
    <style>
        #menu {
            position: absolute;
            z-index: 2;
            padding: 6px 0 6px 10px;
            margin-right: 20px;
            left: 60px;
            top: 10px;
            float: left;
            border-radius: 4px;
            -moz-box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.1);
            -webkit-box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.1);
            box-shadow: 0px 0px 0px 2px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            background: #fff;

        }

        label {
            margin: 0;
            margin-right: 10px;
        }

        .ol-popup {
            position: absolute;
            background-color: white;
            -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
            filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
            padding: 15px;
            border-radius: 10px;
            border: 1px solid #cccccc;
            bottom: 12px;
            left: -50px;
            min-width: 280px;
        }

        .ol-popup:after,
        .ol-popup:before {
            top: 100%;
            border: solid transparent;
            content: " ";
            height: 0;
            width: 0;
            position: absolute;
            pointer-events: none;
        }

        .ol-popup:after {
            border-top-color: white;
            border-width: 10px;
            left: 48px;
            margin-left: -10px;
        }

        .ol-popup:before {
            border-top-color: #cccccc;
            border-width: 11px;
            left: 48px;
            margin-left: -11px;
        }

        .ol-popup-closer {
            text-decoration: none;
            position: absolute;
            top: 2px;
            right: 8px;
        }

        .ol-popup-closer:after {
            content: "✖";
        }
    </style>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
    <div id="map" style="margin:0 auto;width: 100%;height: 100%;"></div>
    <div id="popup" class="ol-popup">
        <a href="#" id="popup-closer" class="ol-popup-closer"></a>
        <div id="popup-content"></div>
    </div>
    <div id='menu' class="mapboxgl-ctrl">
        <input id='basic' type='radio' name='rtoggle' value='basic' checked='checked'>
        <label for='basic' data-i18n="resources.text_vectorDefaultStyle"></label>
        <input id='dark' type='radio' name='rtoggle' value='dark'>
        <label for='dark' data-i18n="resources.text_darkBlueStyle"></label>
        <input id='fiordcolor' type='radio' name='rtoggle' value='fiordcolor'>
        <label for='fiordcolor' data-i18n="resources.text_fiordStyle"></label>
        <input id='klokantech' type='radio' name='rtoggle' value='klokantech'>
        <label for='klokantech' data-i18n="resources.text_klokantechStyle"></label>
        <input id='osm' type='radio' name='rtoggle' value='OSM'>
        <label for='osm' data-i18n="resources.text_osmStyle"></label>
        <input id='positron' type='radio' name='rtoggle' value='positron'>
        <label for='positron' data-i18n="resources.text_positronStyle"></label>
    </div>
    <script type="text/javascript" src="https://iclient.supermap.io/web/libs/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="https://iclient.supermap.io/web/libs/i18next/i18next.min.js"></script>
    <script type="text/javascript" src="https://iclient.supermap.io/web/libs/jquery-i18next/jquery-i18next.min.js"></script>
    <script type="text/javascript" src="../js/utils.js"></script>
    <script type="text/javascript" src=".././locales/zh-CN/resources.js"></script>
    <script type="text/javascript" src="../js/localization.js"></script>
    <script>
        Localization.initializeI18N('../', function () {
            Localization.localize();
            Localization.initGlobal();
        });
    </script>
    <link rel="stylesheet" href="https://iclient.supermap.io/web/libs/bootstrap/css/bootstrap.min.css">
    <script type="text/javascript" include='ol-mapbox-style' src="../../dist/ol/include-ol.js"></script>
    <script type="text/javascript">
        var url = "https://iserver.supermap.io/iserver/services/map-beijing/rest/maps/beijingMap";
        /**
         * Elements that make up the popup.
        */
        var container = document.getElementById('popup');
        var content = document.getElementById('popup-content');
        var closer = document.getElementById('popup-closer');
        /**
         * Create an overlay to anchor the popup to the map.
         */
        var overlay = new ol.Overlay({
            element: container,
            autoPan: true,
            autoPanAnimation: {
                duration: 250
            }
        });


        /**
         * Add a click handler to hide the popup.
         * @return {boolean} Don't follow the href.
         */
        closer.onclick = function () {
            overlay.setPosition(undefined);
            closer.blur();
            return false;
        };

        var stylesJson = {
            'basic': "beijing.json",
            'dark': "beijingDark.json",
            'fiordcolor': "beijingFiord.json",
            'klokantech': "beijingKlokantech.json",
            'osm': "beijingOSM.json",
            'positron': "beijingPositron.json"
        }
        var vectorLayer, currentStyleID, styles = {};
        var selectID = 'basic';
        var map = new ol.Map({
            target: 'map',
            overlays: [overlay],
            // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
            controls: ol.control.defaults.defaults({
                    attributionOptions: {
                        collapsed: false
                    }
                })
                .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
            view: new ol.View({
                maxResolution: 40075016.68557849 / 512,
                center: ol.proj.fromLonLat([116.4, 39.9]),
                zoom: 11
            })
        });
        var targetStyle;
        switchStyle(null, 'basic');
        map.on('click', function (e) {
            var features = map.getFeaturesAtPixel(e.pixel);
            if (features && features.length > 0) {
                var coordinate = e.coordinate;
                content.innerHTML = "Layer: " + features[0].get('layer') + "<br />" +
                    (features[0].get('NAME') ? "Name:  " + features[0].get('NAME') + "<br />" : "") +
                    (features[0].get('道路名称') ? "道路名称:  " + features[0].get('道路名称') + "<br />" : "");
                overlay.setPosition(coordinate);
                targetStyle.setSelectedId(features[0].getId(), features[0].get('layer'));
                vectorLayer.changed();
            }

        });
        var layerList = document.getElementById('menu');
        var inputs = layerList.getElementsByTagName('input');

        for (var i = 0; i < inputs.length; i++) {
            inputs[i].onclick = switchStyle;
        }

        function switchStyle(e, targetStyleID) {
            selectID = targetStyleID || e.target.id;
            if (selectID === currentStyleID) {
                return;
            }
            overlay.setPosition(undefined);
            closer.blur();
            if (styles[selectID]) {
                targetStyle.setStyle(styles[selectID]);
            } else {
                $.get('../data/styles/' + stylesJson[selectID], function (json) {
                    styles[selectID] = json;
                    if (targetStyle) {
                        targetStyle.setStyle(json);
                    } else {
                        targetStyle = new ol.supermap.MapboxStyles({
                            source: 'vector-tiles',
                            map: map,
                            style: json
                        });
                        targetStyle.on('styleloaded', function () {
                            styleLoaded(selectID);
                        });
                    }

                })
            }
        }

        function styleLoaded(styleID) {
            currentStyleID = styleID;
            if (!vectorLayer) {
                var vectorTileOptions = {
                    url: url,
                    format: new ol.format.MVT({
                        featureClass: ol.Feature
                    }),
                    tileGrid:ol.tilegrid.createXYZ({tileSize:512}),
                    tileType: 'ViewBounds'
                };
                vectorLayer = new ol.layer.VectorTile({
                    //设置避让参数
                    declutter: true,
                    style: targetStyle.getStyleFunction(),
                    source: new ol.source.VectorTileSuperMapRest(vectorTileOptions),
                });
                map.addLayer(vectorLayer);
            } else {
                vectorLayer.changed();
            }

        }
    </script>
</body>

</html>